import System

random_1000 = [1000 .. 1]


quicksort [] = []

-- Could have used 'filter' instead of list comprehensions
quicksort (pivot:xs) =
    quicksort [x | x <- xs, x <= pivot] ++
    [pivot] ++
    quicksort [x | x <- xs, x > pivot]


mergesort [] [] = []

mergesort [] x = x

mergesort x [] = x

mergesort (x:xs) (y:ys)
    | x <= y = x : mergesort xs (y:ys)
    | otherwise = y : mergesort (x:xs) ys


{-
    Return a string of the items in the list separate by a comma and space.
-}
commaSeparate :: Show a => [a] -> String

commaSeparate [x] = (show x)

commaSeparate (x:xs) =
    (show x) ++ ", " ++ (commaSeparate xs)


main =
    do
        argv_list <- getArgs
        let list_length = read (head argv_list) :: Int
        let random_ints = reverse [1 .. list_length]
        let first_half = quicksort (take (div list_length 2) random_ints)
        let last_half = quicksort (drop (div list_length 2) random_ints)
        let sorted_list = mergesort first_half last_half
        putStr ("[" ++ (commaSeparate sorted_list) ++ "]\n")
